//
//  SHVisitorView.swift
//  SHWeibo01
//
//  Created by 卫平玉 on 16/7/19.
//  Copyright © 2016年 卫平玉. All rights reserved.
//

import UIKit
import SnapKit
class SHVisitorView: UIView {

    var closure:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    @objc private func loginBtnClick(){
        
        closure?()
    }
    
    func setupVisitorViewInfo(imgName: String?,title: String?){
        
       if let img = imgName,let tit = title{
            
            messageLabel.text = tit;
            
            iconImageView.image = UIImage(named: img)
            
            feedImageView.hidden = true
        }else{
            
            setupAnim()
        }
    }
    
    
    func setupAnim(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = M_PI * 2
        
        anim.duration = 10
        
        anim.removedOnCompletion = false
        
        anim.repeatCount = MAXFLOAT
        
        feedImageView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    func setupUI(){
        
        backgroundColor = UIColor(red: 237/255, green: 237/255, blue: 237/255, alpha: 1)
        addSubview(feedImageView)
        
        addSubview(maskImageView)
        
        addSubview(iconImageView)
        
        addSubview(messageLabel)
        
        addSubview(loginButton)
        
        addSubview(regButton)
        
        
        feedImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 230, height: 36))
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(self)
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        
        regButton.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(loginButton)
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(loginButton)
        }
        
    }
    
    
    private lazy var feedImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_image_smallicon")
    
    private lazy var maskImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_mask_smallicon")
    private lazy var iconImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_image_house")
    
    private lazy var messageLabel: UILabel = {
        
        let lab = UILabel(textColor: UIColor.grayColor(), textFont: 14)
        lab.numberOfLines = 0
        lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lab.textAlignment = .Center
        return lab
    }()
    
    private lazy var loginButton:UIButton = {
        
        let loginBtn = UIButton()
        
        loginBtn.setTitle("登录", forState: UIControlState.Normal)
        loginBtn.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        loginBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return loginBtn
    }()
    
    private lazy var regButton:UIButton = {
        
        let reg = UIButton()
        
        reg.setTitle("注册", forState: UIControlState.Normal)
        reg.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        reg.titleLabel?.font = UIFont.systemFontOfSize(14)
        reg.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        reg.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return reg
    }()
    
}

extension SHVisitorView{
    
    func demo(){
        
        feedImageView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: feedImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: feedImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
    }
}
